//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
import { StaticInstance } from './StaticInstance';
import { ENUM_RESOURCE_TYPE, ENUM_UI_TYPE } from './Enum';
import AudioManager from "./manager/AudioManager";
import DataManager from './manager/DataManager';
import ResourceManager from "./manager/ResourceManager";
import SdkManager from './manager/SdkManager';
import {TimeBasedRequest} from "./Tool/dataUpdater";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Index extends cc.Component {
    @property(cc.Node)
    ErrorShow: cc.Node = null

    onLoad() {
        this.node.getChildByName('UI').opacity = 255
        cc.view.setResizeCallback(() => this.responsive())
        this.responsive()
        DataManager.instance.loadingRate = 0
        cc.macro.ENABLE_MULTI_TOUCH = false
    }

    async start() {
        // 加载资源
        for (const index in ENUM_RESOURCE_TYPE) {
            const resource = ENUM_RESOURCE_TYPE[index]
            await ResourceManager.instance.loadRes(resource)
        }
        // 加载ui
        StaticInstance.uiManager.init()
        // 读档
        DataManager.instance.restore()
        // 播放音乐
        AudioManager.instance.playMusic()
        // 加载sdk
        SdkManager.instance.initAudioEndListener()
        SdkManager.instance.passiveShare()
        SdkManager.instance.getRank()
        SdkManager.instance.initBannerAd()
        SdkManager.instance.initInterstitialAd()
        SdkManager.instance.initVideoAd()
        SdkManager.instance.initCustomRowAd()
        SdkManager.instance.initCustomColAd()
        // 体力恢复逻辑
        this.onPowerResume()
        // 操作ui
        await TimeBasedRequest.checkAndRequestData(4,0, 5, 0).then((status : number | boolean): void=>{
            console.log('返回的状态：' + status)   //1.上架审核中，2.审核通过，3.关闭客户小程序 // 晚上 4:00 到 5:00
            if(status == 3){
                this.ErrorShow.active = true
            }else{ 
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, true, () => {
                    DataManager.instance.loadingRate = 1
                })
            }
        }) 
        
    }

    // 屏幕响应式
    responsive() {
        const designSize = cc.view.getDesignResolutionSize();
        const viewSize = cc.view.getFrameSize();

        const setFitWidth = () => {
            cc.Canvas.instance.fitHeight = false;
            cc.Canvas.instance.fitWidth = true;
        }

        const setFitHeight = () => {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = false;
        }

        const setFitBoth = () => {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = true;
        }

        const designRatio = designSize.width / designSize.height
        const viewRatio = viewSize.width / viewSize.height
        if (designRatio < 1) {
            // console.error('--竖屏游戏')
            if (viewRatio < 1) {
                if (viewRatio > designRatio) {
                    setFitBoth()
                } else {
                    setFitWidth()
                }
            } else {
                setFitBoth()
            }
        } else {
            // console.error('--宽屏游戏')
            if (viewRatio > 1) {
                if (viewRatio < designRatio) {
                    setFitBoth()
                } else {
                    setFitHeight()
                }
            } else {
                setFitBoth()
            }
        }
    }

    onPowerResume() {
        // 体力小于10的时候需要一次性补充
        if (DataManager.instance.power < DataManager.instance.powerMax) {
            const now = new Date().getTime()
            const seconds = Math.floor((now - DataManager.instance.lastPowerUpdateTime) / 1000)
            const powers = Math.floor(seconds / DataManager.instance.powerRefreshTime)
            if (DataManager.instance.power + powers >= DataManager.instance.powerMax) {
                DataManager.instance.power = DataManager.instance.powerMax
                DataManager.instance.lastPowerRefreshTime = 0
            } else {
                DataManager.instance.power += powers
            }
            DataManager.instance.save()
        }
    }
}
